import { Base64 } from 'js-base64'
import { ClassicModel } from '../../models/classic.js'
import { LikeModel } from '../../models/like.js'
let classicModel = new ClassicModel()
let likeModel = new LikeModel()

Page({
  data: {
    classic: null,
    latest: true,
    first: false,
    likeCount: 0,
    likeStatus: false
  },

  onLoad: function () {
    classicModel.getLatest((res) => {
      console.log('onLoad', res.id, res.type)
      this._getLikesStatus(res.id, res.type)
      this.setData({
        classic: res,
      })
    })
  },

  onLike(e) {
    let behavior = e.detail.behavior
    console.log('onLike', this.data.classic.id, this.data.classic.type)
    likeModel.like(behavior, this.data.classic.id, 
    this.data.classic.type)
  },

  onNext(event) {
    this._updateClassic('next')
  },

  onPrevious(event) {
    this._updateClassic('previous')
  },

  _updateClassic(nextOrPrevious) {
    let index = this.data.classic.index
    classicModel.getClassic(index, nextOrPrevious, (res) => {
      console.log('_updateClassic like', res.id, res.type)
      this._getLikesStatus(res.id, res.type)
      this.setData({
        classic: res,
        latest: classicModel.isLatest(res.index),
        first: classicModel.isFirst(res.index)
      })
    })
  },

  _getLikesStatus(artID, category) {
    likeModel.getClassicLikeStatus(artID, category,
    (res) => {
      console.log('_getLikesStatus', res)
      this.setData({
        likeCount: res.fav_nums,
        likeStatus: res.like_status
      })
    })
  }

})
